export default function dateFormat(dateVal: string) {
    let date = null;
    if (dateVal) {
        date = new Date(Date.parse(dateVal))
    } else {
        date = new Date()
    }
    const year = date.getFullYear().toString().padStart(4, '0');
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

export function dateFormatYMD(dateVal: string) {
    let date = null;
    if (dateVal) {
        date = new Date(Date.parse(dateVal))
    } else {
        date = new Date()
    }
    const year = date.getFullYear().toString().padStart(4, '0');
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`
}

export function getTimeDifference(startTime: string, endTime: string) {
    const startTimeNum = Date.parse(startTime)
    const endTimeNum = Date.parse(endTime)
    return formatTimeDifference(endTimeNum - startTimeNum)
}

function formatTimeDifference(timeDifference: number) {
    const hour = Math.floor((timeDifference / (1000 * 60 * 60)) % 24);
    const minute = Math.floor((timeDifference / (1000 * 60)) % 60)
    const second = Math.floor((timeDifference / 1000) % 60)
    return hour > 0 ? `${padStartNum(hour)}:${padStartNum(minute)}:${padStartNum(second)}` : (minute > 0 ? `${padStartNum(minute)}分${padStartNum(second)}秒` : `${padStartNum(second)}秒`)
}

function padStartNum(num: number) {
    return num.toString().padStart(2, '0')
}